package com.gitee.qdbp.general.mgr.controller.permission;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.result.ResponseMessage;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.base.annotation.OperateRecord;
import com.gitee.qdbp.base.enums.OperateType;
import com.gitee.qdbp.base.shiro.mgt.OnlineManager;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.base.utils.EnumTools;
import com.gitee.qdbp.base.utils.SessionTools;
import com.gitee.qdbp.general.system.api.permission.model.ResourceCascadeBean;
import com.gitee.qdbp.general.system.api.permission.model.ResourceCoreBean;
import com.gitee.qdbp.general.system.api.permission.model.ResourceCoreUpdate;
import com.gitee.qdbp.general.system.api.permission.model.ResourceCoreWhere;
import com.gitee.qdbp.general.system.api.permission.service.IResourceCoreExecutor;
import com.gitee.qdbp.general.system.api.permission.service.IResourceCoreQueryer;
import com.gitee.qdbp.general.system.enums.SysResState;
import com.gitee.qdbp.general.system.enums.SysResType;
import com.gitee.qdbp.tools.utils.ConvertTools;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 资源基本信息控制器
 *
 * @author zhaohuihua
 * @version 170503
 */
@Controller
@RequestMapping("/actions/resource")
public class ResourceController {

    @Autowired
    private IResourceCoreQueryer resourceCoreQueryer;
    @Autowired
    private IResourceCoreExecutor resourceCoreExecutor;
    @Autowired(required = false)
    private OnlineManager onlineManager;

    /**
     * 查询资源列表
     *
     * @param paging 分页信息
     * @param model 查询条件
     * @return 资源列表
     */
    @ResponseBody
    @RequestMapping("list")
    @OperateRecord(value = "权限资源:查询", type = OperateType.QUERY)
    // @RequiresPermissions("resource:manager:query") // 改为"resource:" + sceneType + ":query"
    public ResponseMessage list(ResourceCoreWhere model, OrderPaging paging, Boolean extra) {
        if (VerifyTools.isBlank(model.getSceneType())) {
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        SecurityUtils.getSubject().isPermitted("resource:" + model.getSceneType() + ":query");

        try {
            PageList<ResourceCoreBean> list = resourceCoreQueryer.list(model, paging);
            ResponseMessage result = new ResponseMessage(list.toList());
            if (Boolean.TRUE.equals(extra)) {
                result.addExtra(EnumTools.getEnumsResource(SysResType.class, SysResState.class));
            }
            return result;
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 创建资源 **/
    @ResponseBody
    @RequestMapping("create")
    @OperateRecord(value = "权限资源:创建", type = OperateType.CREATE)
    public ResponseMessage create(ResourceCascadeBean model) {
        if (VerifyTools.isBlank(model.getSceneType())) {
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        SecurityUtils.getSubject().isPermitted("resource:" + model.getSceneType() + ":create");

        try {
            IAccount operator = SessionTools.getLoginUser();
            resourceCoreExecutor.create(model, operator);
            return new ResponseMessage(model);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 修改资源 **/
    @ResponseBody
    @RequestMapping("update")
    @OperateRecord(value = "权限资源:修改", type = OperateType.UPDATE)
    public ResponseMessage update(ResourceCoreUpdate model) {
        if (VerifyTools.isBlank(model.getSceneType())) {
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        SecurityUtils.getSubject().isPermitted("resource:" + model.getSceneType() + ":update");

        try {
            IAccount operator = SessionTools.getLoginUser();
            resourceCoreExecutor.update(model, operator);
            return new ResponseMessage(model);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 删除资源 **/
    @ResponseBody
    @RequestMapping("delete")
    @OperateRecord(value = "权限资源:删除", type = OperateType.DELETE)
    public ResponseMessage delete(String sceneType, String id, Boolean cascade) {
        if (VerifyTools.isBlank(sceneType)) {
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        SecurityUtils.getSubject().isPermitted("resource:" + sceneType + ":delete");

        try {
            IAccount operator = SessionTools.getLoginUser();
            resourceCoreExecutor.delete(sceneType, ConvertTools.toList(id), Boolean.TRUE.equals(cascade), operator);
            return new ResponseMessage();
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 禁用资源 **/
    @ResponseBody
    @RequestMapping("disable")
    @OperateRecord(value = "权限资源:禁用", type = OperateType.UPDATE)
    public ResponseMessage disable(String sceneType, String id) {
        if (VerifyTools.isBlank(sceneType)) {
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        SecurityUtils.getSubject().isPermitted("resource:" + sceneType + ":disable");

        try {
            IAccount operator = SessionTools.getLoginUser();
            resourceCoreExecutor.disable(sceneType, ConvertTools.toList(id), operator);
            return new ResponseMessage();
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 启用资源 **/
    @ResponseBody
    @RequestMapping("enable")
    @OperateRecord(value = "权限资源:启用", type = OperateType.UPDATE)
    public ResponseMessage enable(String sceneType, String id, Boolean cascade) {
        if (VerifyTools.isBlank(sceneType)) {
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }
        SecurityUtils.getSubject().isPermitted("resource:" + sceneType + ":enable");

        try {
            IAccount operator = SessionTools.getLoginUser();
            resourceCoreExecutor.enable(sceneType, ConvertTools.toList(id), Boolean.TRUE.equals(cascade), operator);
            return new ResponseMessage();
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 重新加载当前用户的授权信息 **/
    @ResponseBody
    @RequestMapping("reload")
    @OperateRecord(value = "权限资源:重新加载", type = OperateType.UPDATE)
    // @RequiresPermissions("resource:manager:reload") // 改为"resource:" + sceneType + ":reload"
    public ResponseMessage reload(String sceneType) {
        SecurityUtils.getSubject().isPermitted("resource:" + sceneType + ":reload");
        if (onlineManager == null) {
            return new ResponseMessage(ResultCode.UNSUPPORTED_OPERATION);
        } else {
            onlineManager.clearMyAuthorizationInfo();
            return new ResponseMessage();
        }
    }
}
